<?php

namespace App\Services;

use App\Models\Question;
use App\Models\QuestionOption;

class QuestionService extends BaseService
{
    /**
     * 获取列表
     * @return
     */
    public function list($page,$limit,$sort,$order,$name,$question_category_id) {
        $query = Question::leftJoin('question_categories', 'question_categories.id', '=', 'questions.question_category_id')
            ->select('questions.*',
                'question_categories.name as categoriesName'
            );
        if(!empty($name)){
            $query->where('questions.name','like',"%$name%");
        }
        if(!empty($question_category_id)){
            $query->where('question_category_id',$question_category_id);
        }

        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取试题详情
     * @return
     */
    public function info($id) {
        $query = Question::where('id',$id)->first();
        if($query){
            $questionOption = QuestionOption::where('question_id',$id)->orderBy('order','asc')->get();
            $query->questionOption = $questionOption;
        }
        return $query;
    }
}
